layui.use(['form', 'jquery'], () => {
    let form = layui.form,
        $ = layui.jquery;
    form.verify({
        username: function (value, item) { //value：表单的值、item：表单的DOM对象
            if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                return '用户名不能有特殊字符';
            }
            if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                return '用户名首尾不能出现下划线\'_\'';
            }
            if (/^\d+\d+\d$/.test(value)) {
                return '用户名不能全为数字';
            }
        },
        pass: [
            /^[\S]{6,12}$/
            , '密码必须6到12位，且不能出现空格'
        ],
        repass: (value, item) => {
            if (value !== $("input[name='password']").val()) {
                return "两次输入的密码不一致";
            }
        }
    });
    form.on('submit(register)', (elem) => {
        let data = elem.field;
        ajax("/back/admin/register", "POST", data, (res) => {
            if (res.code === 0) {
                alert("注册成功，点击跳转到到登录页面...");
                window.location.replace("/login");
            } else {
                alert(res.msg);
                registerForm.reset();
            }
        });
    });
});


function init() {
    $(".register-button-box button").click(submit);
}

function submit() {
    $.ajax({
        url: crossOrigin + "/back/admin/register",
        data: {"name": name, "username": username, "password": password, "verifyPassword": verifyPassword},
        success: (ret) => {
            if (ret.code === 0) {
                alert("注册成功，点击跳转到到登录页面...");
                window.location.href = "/login";
            } else {
                alert(ret.msg);
                registerForm.reset();
            }
        }
    });

}